package dto;

// Generated May 6, 2011 4:19:20 PM by Hibernate Tools 3.3.0.GA

import SessionFactory.SessionFactoryUtil;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Posts.
 * @see dto.Posts
 * @author Hibernate Tools
 */
public class PostsHome {

	private static final Log log = LogFactory.getLog(PostsHome.class);

	private final SessionFactory sessionFactory = SessionFactoryUtil.getInstance();


	public void persist(Posts transientInstance) {
		log.debug("persisting Posts instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Posts instance) {
		log.debug("attaching dirty Posts instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Posts instance) {
		log.debug("attaching clean Posts instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Posts persistentInstance) {
		log.debug("deleting Posts instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Posts merge(Posts detachedInstance) {
		log.debug("merging Posts instance");
		try {
			Posts result = (Posts) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Posts findById(Integer id) {
		log.debug("getting Posts instance with id: " + id);
		try {
			Posts instance = (Posts) sessionFactory.getCurrentSession().get(
					"dto.Posts", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Posts> findByExample(Posts instance) {
		log.debug("finding Posts instance by example");
		try {
			List<Posts> results = (List<Posts>) sessionFactory
					.getCurrentSession().createCriteria("dto.Posts").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
        public List<Posts> getPostsInTopics(Vector<Integer> mergedTopicsIds) {
        Query qry = sessionFactory.getCurrentSession().createQuery("from Posts where topicid IN :id");

        qry.setParameterList("id", mergedTopicsIds);

        List<Posts> posts = qry.list();

        return posts;
    }

    public int movePosts(Vector<Integer> mergedTopicsIds, Integer newTopicId) {
        Query qry = sessionFactory.getCurrentSession().createQuery(
                        "update Posts set topicId = :tId where topicId in (:id) ");

        qry.setParameterList("id", mergedTopicsIds);
        qry.setParameter("tId", newTopicId);

        return qry.executeUpdate();
    }

    public int deletePostsBeforeDate(Integer topicId, Date beforeDate) {

//      session.beginTransaction();
        Query qry = sessionFactory.getCurrentSession().createQuery(
                        "delete from Posts where topicId = :id AND postdate < :date");

        qry.setInteger("id", topicId);
        qry.setDate("date", beforeDate);

        int count = qry.executeUpdate();

        System.out.println(count);

        return count;

//      session.getTransaction().commit();
    }

    public List<Posts> getPostsInTopic(Integer topicid) {
        Query qry = sessionFactory.getCurrentSession().createQuery("from Posts where topicId = :id");

        qry.setInteger("id", topicid);

        return qry.list();
    }
}
